package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
@Controller
@RequestMapping
public class SearchController {
    @RequestMapping("search")
    public String search(String keyword, String sort, Model model) throws Exception {
        if(keyword.length()==0) {
            return "search";
        }
//        List<Product> products = productService
//                .list("name_like",keyword,"order",handleSort(sort),"stock_gt",0);
//        model.addAttribute("products",products);
//        model.addAttribute("keyword",keyword);
        return "search";
    }

    private String handleSort(String sort){
        sort = sort==null?"":sort;
        String column = "";
        String order = "desc";
        switch(sort){
            case "date":
                column = "createDate";
                break;
            case "comment":
                column = "commentCount";
                break;
            case "saleCount":
                column = "saleCount";
                break;
            case "price":
                column = "nowPrice";
                order = "asc";
                break;
            case "priceInverse":
                column = "nowPrice";
                break;
            default:
                column = "commentCount";
                break;
        }
        return String.format("%s %s , id desc",column,order);
    }
}
